package com.hjh.testdemo.ui.usb.host

import android.content.Context
import android.hardware.usb.UsbManager
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.drake.brv.utils.addModels
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.hjh.core.activity.BaseFragment
import com.hjh.testdemo.R
import com.hjh.testdemo.databinding.FragmentUsbScanBinding
import com.hjh.testdemo.databinding.ItemUsbDeviceBinding
import com.hjh.testdemo.ui.usb.host.CustomProber.getCustomProber
import com.hoho.android.usbserial.driver.UsbSerialDriver
import com.hoho.android.usbserial.driver.UsbSerialProber

/**
 * @author： hjh
 * @createDate： 2025/6/17 11:40
 * @description：
 * @qq： 3472903821
 * @version: 1.0
 */
class UsbScanFragment : BaseFragment<FragmentUsbScanBinding>() {

    private lateinit var usbManager: UsbManager
    private val viewModel by viewModels<UsbHostViewModel>()

    override fun initView() {
        usbManager = requireContext().getSystemService(Context.USB_SERVICE) as UsbManager
    }

    override fun initRecycleView() {
        binding.recyclerDevices.linear().setup {
            addType<UsbSerialDriver>(R.layout.item_usb_device)

            onBind {
                getBindingOrNull<ItemUsbDeviceBinding>()?.run {
                    getModelOrNull<UsbSerialDriver>()?.let { driver ->
                        tvDeviceTitle.text = driver.device.deviceName
                        tvDeviceInfo.text = getString(
                            com.hjh.res.R.string.usb_device_info,
                            driver.device.vendorId,
                            driver.device.productId
                        )
                    }
                }
            }

            onClick(R.id.btn_comm) {
                getModelOrNull<UsbSerialDriver>()?.let { driver ->
                    viewModel.updateUsbSerialDriver(driver)
                    findNavController().navigate(R.id.usbHostFragment)
                }
            }

            onClick(R.id.btn_info) {
                getModelOrNull<UsbSerialDriver>()?.let { driver ->
                    viewModel.updateUsbSerialDriver(driver)
                    findNavController().navigate(R.id.usbDeviceInfoFragment)
                }
            }
        }
    }

    override fun initListener() {
        binding.btnScan.setOnClickListener {
            binding.recyclerDevices.models = emptyList()
            val usbDefaultProber = UsbSerialProber.getDefaultProber()
            val usbCustomProber = getCustomProber()
            usbManager.deviceList.values.forEach {
                var usbDriver = usbDefaultProber.probeDevice(it)
                if (usbDriver == null) {
                    usbDriver = usbCustomProber.probeDevice(it)
                }
                if (usbDriver != null) {
                    binding.recyclerDevices.addModels(listOf(usbDriver))
                }
            }
        }
    }
}
